﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Reflection;
using System.Globalization;

namespace AppLibrary.Storage
{
    [Serializable]
    public class ReferenceViolationException : Exception
    {
        private PropertyInfo referenceProperty;

        public ReferenceViolationException(PropertyInfo referenceProperty)
            : this(referenceProperty, GetDefaultMessage(referenceProperty))
        {
        }

        public ReferenceViolationException(PropertyInfo referenceProperty, String message)
            : this(referenceProperty, message, null)
        {
        }

        public ReferenceViolationException(PropertyInfo referenceProperty, String message, Exception innerException)
            : base(message, innerException)
        {
            this.referenceProperty = referenceProperty;
        }

        public ReferenceViolationException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }

        public PropertyInfo ReferenceProperty
        {
            get { return this.referenceProperty; }
        }

        private static String GetDefaultMessage(PropertyInfo referenceProperty)
        {
            return String.Format(
                CultureInfo.CurrentCulture,
                "This {0} is referenced by {1}.{2}.",
                referenceProperty.PropertyType,
                referenceProperty.DeclaringType,
                referenceProperty.Name);
        }
    }
}
